<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2019/12/2
 * Time: 23:11
 */
namespace app\api\controller;

use app\api\model\Login as ApiLoginModel;
use app\admin\model\Login as LoginModel;

class Login extends Controller
{
    public function test()
    {
        return $this->renderSuccess([], '请求成功');
    }

    public function index()
    {
        $data = $this->request->get();
        return $this->json(1001, '登录成功', $data);
    }

    public function login_new($username, $password)
    {
        $model = new ApiLoginModel();
        $data = $model->login($username, $password);
        if (empty($data['token'])) {
            return $this->renderError($data['msg']);
        }
        return $this->renderSuccess(['token' => $data['token'], 'data' => $data['data']], $data['msg']);
    }

    public function login($username, $password)
    {
        $user = LoginModel::get(['Aname' => $username]);
        if (empty($user)){
            return $this->renderError('用户不存在');
        }
        if ($user['Apassword'] == $password) {
            session('username', $username);
            return $this->renderSuccess([], '登录成功');
        } else {
            return $this->renderError('用户或密码不正确');
        }
    }
}
